import axios from "axios";
import {MyUrl} from "./Constant.ts";

// @ts-ignore
export function hook_getSongByUserId(userId: number): Promise<Array> {
    return axios.get(MyUrl.SONG_getSongList, {
        params: {
            userId: userId,
        }
    }).then(res => {
        // console.log(JSON.stringify(res.data.data))
        if (res.data.flag) {
            return res.data.data
        } else {
            return []
        }
    }).catch(error => {
        console.error("获取歌曲列表失败：" + error)
        return []
    })
}

export function hook_getSongPage(userId: number, currentPage: number, pageSize: number) {
    return axios.get(MyUrl.SONG_getSongPage, {
        params: {
            userId: userId,
            currentPage: currentPage,
            pageSize: pageSize,
        }
    }).then(res => {
        // console.log(JSON.stringify(res.data.data))
        return res.data.data
    }).catch(error => {
        console.error("获取歌曲列表失败：" + error)
        return []
    })
}

export function hook_deleteSong(id: number, imgName: string) {
    return axios.delete(MyUrl.SONG_deleteSong, {
        params: {
            id: id,
            imgName: imgName,
        }
    }).then(res => {
        if (res.data.flag) {
            return res.data.data
        }
    }).catch(error => {
        console.error("删除歌曲失败：" + error)
    })
}

//根据id获取歌曲信息
export function hook_findSongById(id: number) {
    return axios.get(MyUrl.SONG_findSongById, {
        params: {
            id: id,
        }
    }).then(res => {
        if (res.data.flag) {
            return res.data.data
        }
    }).catch(error => {
        console.error("获取歌曲失败：" + error)
    })
}

//保存String到File
export function hook_stringToFile(input: string, fileName: string) {
    const blob = new Blob([input], {
        type: 'text/plain;charset=utf-8',
    })

    // downLoadFile(file)
    return new File([blob], fileName)
}

//下载file
export function hook_downloadFile(file: File) {
    let url = URL.createObjectURL(file);
    let link = document.createElement('a');

    link.href = url;
    link.download = file.name;
    document.body.appendChild(link);
    link.click()
    document.body.removeChild(link);

    URL.revokeObjectURL(url);
}
//根据playlistId查找所对应的歌曲
export function hook_findSongByPlaylistId(playlistId: number) {
    return axios.get(MyUrl.SONG_findSongByPlaylistId, {
        params: {
            playlistId: playlistId,
        }
    }).then(res => {
        console.log(res.data)
        if (res.data.flag) {
            return res.data.data
        }
    }).catch(error => {
        console.error("查找歌曲失败：" + error)
    })
}

//根据str 模糊查询歌曲
export async function hook_findSongByKeyword(keyword: string) {
    return await axios.get(MyUrl.SONG_findSongByKeyword, {
        params: {
            keyword: keyword,
        }
    }).then(res => {
        // console.log(res.data)
        if (res.data.flag) {
            return res.data.data
        }
    }).catch(error => {
        console.error("查找歌曲失败：" + error)
    })
}

//根据str 模糊查询歌曲页面
export async function hook_getSongPageByKeyword(keyword: string, pages: number) {
    return await axios.get(MyUrl.SONG_getSongPageByKeyword, {
        params: {
            keyword: keyword,
            pages: pages
        }
    }).then(res => {
        // console.log(res.data)
        if (res.data.flag) {
            return res.data.data
        }
    }).catch(error => {
        console.error("查找歌曲页面失败：" + error)
    })
}

//查询最新的歌曲
export async function hook_getNewSongFromEs(number: number) {
    return await axios.get(MyUrl.SONG_getNewSongFromEs, {
        params: {
            number:number
        }
    }).then(res => {
        // console.log(res.data)
        if (res.data.flag) {
            return res.data.data
        }
    }).catch(error => {
        console.error("查找歌曲页面失败：" + error)
    })
}


